#include <func.h>
#include "account.h"
#include "client.h"
int randSalt(char **salt){
    //salt的格式为 $6$ + 8个字符 + $
    srand(time(NULL));//通过时间函数设置随机
    char str[SALTLEN] = {0};
    for(int i = 0;i < SALTLEN; i++){
        int flag = rand() %3;
        switch (flag)
        {
        case 0:
            str[i] = rand()%26 + 'A';
            break;
        case 1:
            str[i] = rand()%26 + 'a';
            break;
        case 2:
            str[i] = rand()%10 + '0';
            break;
        default:
            break;
        }
    }
    strcpy(*salt,"$6$");
    strcat(*salt,str);
    strcat(*salt,"$");
}

int getSaltCrypt(char *name,char **salt,char **crypt){
    //进入对应数据库
    MYSQL *db = mysql_init(NULL);
    char * host = "localhost";
    char * user = "root";
    char * password = "123";
    char * database = "netDisk";
    MYSQL * ret = mysql_real_connect(db,host,user,password,database,0,NULL,0);
    if(ret != 0){
        printf("db = %p,ret = %p\n",db,ret);
        printf("successed\n");
    }
    else{
        printf("error: %s\n",mysql_error(db));
        exit(-1);
    }
    //选择执行语句
    char sql[300] = "select salt,crypt from account where user = ";
    strcat(sql,"'");
    strcat(sql,name);
    strcat(sql,"'");
    int qret = mysql_query(db,sql);
    if(qret != 0){
        printf("error: %s\n",mysql_error(db));
        exit(-1);
    }
    MYSQL_RES *result = mysql_store_result(db);
    //printf("the row number = %ld\n",mysql_num_rows(result));
    MYSQL_ROW  row;
    if((row = mysql_fetch_row(result)) != NULL){
            strcpy(*salt,row[0]);
            strcpy(*crypt,row[1]);
    }
    mysql_free_result(result);
    mysql_close(db);
}

int saveSaltCrypt(char *name,char *salt,char *crypt){
    //进入对应数据库
    MYSQL *db = mysql_init(NULL);
    char * host = "localhost";
    char * user = "root";
    char * password = "123";
    char * database = "netDisk";
    MYSQL * ret = mysql_real_connect(db,host,user,password,database,0,NULL,0);
    if(ret != 0){
        printf("db = %p,ret = %p\n",db,ret);
        printf("successed\n");
    }
    else{
        printf("error: %s\n",mysql_error(db));
        exit(-1);
    }
    //选择执行语句
    char str[50] = "insert into account values";
    char sql[300] = {0};
    sprintf(sql,"%s ('%s','%s','%s')",str,name,salt,crypt);
    int qret = mysql_query(db,sql);
    if(qret != 0){
        printf("error: %s\n",mysql_error(db));
        exit(-1);
    }
    mysql_close(db);
}

int loadSever(int netFd,char **username){
    int length;
    //接收账号名字
    char name[50] = {0};
    recv(netFd,&length,sizeof(int),0);
    recv(netFd,name,length,0);
    char pwd[50] = {0};
    //接收账号密码
    recv(netFd,&length,sizeof(int),0);
    recv(netFd,pwd,length,0);
    char *salt = (char *)calloc(50,sizeof(char));
    char *cryp = (char *)calloc(100,sizeof(char));
    char buf[30] = {0};
    getSaltCrypt(name,&salt,&cryp);
    if(strcmp(cryp,crypt(pwd,salt)) == 0){
        strcpy(buf,"true");
        //服务端保存当前用户名
        strcpy(*username,name);
    }
    else{
        strcpy(buf,"false");
    }
    free(salt);
    free(cryp);
    send(netFd,buf,strlen(buf),0);
}

int loadClient(int netFd,char **username){
  //登录模块
    train_t t;
    bool across = false;
    do{
        char name[50] = {0};
        printf("请输入账号名称: ");
        scanf("%s",name);
        char pwd[1024]={0};
        printf("请输入服务器密码: ");
        int i;
        scanf("%s",pwd);
        char rbuf[50]  ={0};
        //发送账号名称
        bzero(&t,sizeof(t));
        t.dataLength = strlen(name);
        strcpy(t.buf,name);
        send(netFd,&t,sizeof(t.dataLength) + t.dataLength,0);
        //发送账号密码
        bzero(&t,sizeof(t));
        t.dataLength = strlen(pwd);
        strcpy(t.buf,pwd);
        send(netFd,&t,sizeof(t.dataLength) + t.dataLength,0);
        //接收是否成功登录消息
        recv(netFd,rbuf,sizeof(rbuf),0);
        if(across == false){
            if(strcmp("true",rbuf)==0){
                across = true;
                //记录当前用户的username
                strcpy(*username,name);
                puts("成功登录！");
            }
            else{
                puts("账号名称错误或密码错误");
                while(1){
                    puts("1.继续登录");
                    puts("2.注册账号");
                    puts("3.退出");
                    int num;
                    scanf("%d",&num);
                    if(num == 1){
                        break;
                    }
                    else if(num == 2){
                        signIn(netFd);
                        break;
                    }
                    else if(num == 3){
                        puts("欢迎下次使用！");
                        exit(0);
                    }
                    else{
                        puts("错误指令，请重新输入");
                    }
                }
            }
        }
    }while(across == false); //登录判断
}

int signIn(int netFd){
    printf("请输入账号名称: ");
    char name[50] = {0};
    scanf("%s",name);
    printf("请输入账号密码: ");
    char pwd[50] = {0};
    scanf("%s",pwd);
    char *salt = (char *)calloc(30,sizeof(char));
    randSalt(&salt);
    //名字，盐值，生成的密文保存到数据库中
    saveSaltCrypt(name,salt,crypt(pwd,salt));
    printf("注册成功\n");
    free(salt);
}
